package com.zzc.interview.model.vo;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.annotation.TableField;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.zzc.interview.model.dto.ojQuestion.JudgeConfig;
import com.zzc.interview.model.entity.OjQuestion;
import com.zzc.interview.model.entity.Question;
import lombok.Data;
import org.springframework.beans.BeanUtils;
import org.springframework.format.annotation.DateTimeFormat;

import java.util.Date;
import java.util.List;

@Data
public class OjQuestionVO {

    /**
     * id
     */
    private Long id;

    /**
     * 标题
     */
    private String title;

    /**
     * 内容
     */
    private String content;

    /**
     * 标签列表（json 数组）
     */
    private List<String> tags;


    /**
     * 题目提交数
     */
    private Integer submitNum;

    /**
     * 题目通过数
     */
    private Integer acceptNum;

    /**
     * 判题配置
     */
    private JudgeConfig judgeConfig;

    /**
     * 点赞数
     */
    private Integer thumbNum;

    /**
     * 收藏数
     */
    private Integer favourNum;

    /**
     * 创建用户 id
     */
    private Long userId;

    /**
     * 创建时间
     */
    @JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd")
    @DateTimeFormat(pattern="yyyy-MM-dd")
    private Date createTime;

    /**
     * 更新时间
     */
    private Date updateTime;

    /**
     * 是否删除
     */
    private Byte isDelete;

    /**
     * 用户提交信息时携带的信息
     */
    @TableField(exist = false)
    private UserVO userVO;

    @TableField(exist = false)
    private static final long serialVersionUID = 1L;

    public static OjQuestionVO objToVo(OjQuestion question) {
        OjQuestionVO vo = new OjQuestionVO();
        BeanUtils.copyProperties(question, vo);
        // tags
        List<String> list = JSONUtil.toList(question.getTags(), String.class);
        vo.setTags(list);
        // judgeConfig
        JudgeConfig judgeConfig = JSONUtil.toBean(question.getJudgeConfig(), JudgeConfig.class);
        vo.setJudgeConfig(judgeConfig);
        return vo;
    }

    public static OjQuestion voToObj(OjQuestionVO questionVO) {
        OjQuestion question = new OjQuestion();
        BeanUtils.copyProperties(questionVO, question);
        // tags
        String tags = JSONUtil.toJsonStr(questionVO.getTags());
        question.setTags(tags);
        // judgeConfig
        String judgeConfig = JSONUtil.toJsonStr(questionVO.getJudgeConfig());
        question.setJudgeConfig(judgeConfig);
        return question;
    }
}
